package com.caifeng.linklist;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 通讯录
 * 增删改查
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button delete, update, query, insert;
    EditText e_name, e_number;
    SQLiteDatabase db;
    String name, phone;
    ContentValues values;
    MyHelper myHelper;
    TextView showList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        e_name = findViewById(R.id.tel_name);
        e_number = findViewById(R.id.tel_number);
        delete = findViewById(R.id.delete);
        update = findViewById(R.id.update);
        insert = findViewById(R.id.add);
        query = findViewById(R.id.query);
        showList = findViewById(R.id.show);
        delete.setOnClickListener(this);
        update.setOnClickListener(this);
        insert.setOnClickListener(this);
        query.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myHelper = new MyHelper(this);
        switch (v.getId()) {
            //添加
            case R.id.add:
                name = e_name.getText().toString();
                phone = e_number.getText().toString();
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name", name);
                values.put("phone", phone);
                db.insert("information", null, values);
                Toast.makeText(this, "联系人已添加", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            //删除
            case R.id.delete:
                //一行一行删除
                db = myHelper.getWritableDatabase();
                Cursor c = db.query("information", null, null, null, null, null, null);
                if (c.getCount() == 0) {
                    Toast.makeText(this, "没有可删除的联系人", Toast.LENGTH_SHORT).show();
                } else {
                    //删除第一行联系人
                    String line = String.valueOf(c.getCount());
                    Log.e("删除", "第"+line+"行");
                    db.delete("information", "_id=?", new String[]{line});
                    Toast.makeText(this, "联系人已删除", Toast.LENGTH_SHORT).show();
                    c.close();
                }
                queryData();

                //db.close();

                break;
            //查询
            case R.id.query:
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information", null, null, null, null, null, null);
                if (cursor.getCount() == 0) {
                    showList.setText("");
                    Toast.makeText(this, "没有联系人", Toast.LENGTH_SHORT).show();

                } else {
                    cursor.moveToFirst();
                    showList.setText("Name: " + cursor.getString(1) +
                            "\tTel: " + cursor.getString(2));
                }
                while (cursor.moveToNext()) {
                    showList.append("\n" + "Name: " + cursor.getString(1) +
                            "\tTel: " + cursor.getString(2));
                }
                cursor.close();
                db.close();

                break;
            //更新
            case R.id.update:
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("phone", e_number.getText().toString());
                db.update("information", values, "name=?", new String[]{e_name.getText().toString()});
                Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
                db.close();

                break;
        }
    }

    public void queryData() {
        Cursor cursor = db.query("information", null, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            showList.setText("");
            Toast.makeText(this, "没有联系人", Toast.LENGTH_SHORT).show();

        } else {
            cursor.moveToFirst();
            showList.setText("Name: " + cursor.getString(1) +
                    "\tTel: " + cursor.getString(2));
        }
        while (cursor.moveToNext()) {
            showList.append("\n" + "Name: " + cursor.getString(1) +
                    "\tTel: " + cursor.getString(2));
        }
        cursor.close();
        db.close();
    }
}
